﻿<template>
  <div class="projectResult-container">
    <el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
      <TableSearch :search="tb.tableData.search" @search="onSearch" />
    </el-card>
    <el-card class="full-table" shadow="hover" style="margin-top: 8px">
      <Table ref="tableRef" v-bind="tb.tableData" :getData="getData" :exportChangeData="exportChangeData"
        @sortHeader="onSortHeader" @selectionChange="tableSelection" border="">
        <template #command="">
          <el-button type="primary" icon="ele-Plus" @click="openaddprojectResult" v-auth="'projectResult:add'"> {{
            t('message.pageConfiguration.add') }} </el-button>
          <el-button v-if="state.selectlist.length > 0" type="danger" icon="ele-Delete" @click="bacthDelete"
            v-auth="'projectResult:batchDelete'"> {{ t('message.pageConfiguration.batchDel') }} </el-button>
          <el-button type="primary" icon="ele-Download" @click="downTemplete" v-auth="'projectResult:upload'"> {{
            t('message.pageConfiguration.downloadTemplate') }} </el-button>
          <el-button type="primary" icon="ele-Plus" @click="openUploadDialog" v-auth="'projectResult:upload'"> {{
            t('message.pageConfiguration.upload') }} </el-button>
        </template>
        <template #action="scope">
          <el-button icon="ele-Edit" size="small" text="" type="primary" @click="openedit(scope.row)"
            v-auth="'projectResult:edit'"> {{ t('message.pageConfiguration.edit') }} </el-button>
            
          <el-button icon="ele-details" size="small" text="" type="primary" @click="opendetail(scope.row)"
            v-auth="'projectResult:edit'"> {{ t('message.pageConfiguration.details') }} </el-button>
          <el-button icon="ele-Delete" size="small" text="" type="danger" @click="delConfig(scope.row)"
            v-auth="'projectResult:delete'"> {{ t('message.pageConfiguration.del') }} </el-button>
        </template>
      </Table>
    </el-card>

    <editDialog ref="editDialogRef" :title="state.editprojectResultTitle" :groupList="state.groupList"
      @updateData="updateData" />
    <Detail ref="DetailRef" :title="state.editprojectResultTitle" />

    <uploaddialog ref="uploaddialogRef" :import-data="importData" />
  </div>
</template>

<script lang="ts" setup="" name="projectResult">
import { onMounted, onUnmounted, reactive, ref, defineAsyncComponent, nextTick, watch } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import mittBus from '/@/utils/mitt';
import editDialog from '/@/views/main/projectResult/component/editDialog.vue'
import Detail from '/@/views/main/projectResult/component/Detail.vue'
import { getDictDataItem as di, getDictDataListToSelect as dl } from '/@/utils/dict-utils';
import { pageProjectResult, deleteProjectResult, batchDeleteProjectResult, importProjectResult, downLoadProjectResult } from '/@/api/main/projectResult';
import { getAPI } from '/@/utils/axios-utils';
import { SysEnumApi } from '/@/api-services/api';
import commonFunction from '/@/utils/commonFunction';
import { auth } from '/@/utils/authFunction'
import { useI18n } from 'vue-i18n';
const { t, locale } = useI18n();
const { downFile } = commonFunction();
// 引入组件
const Table = defineAsyncComponent(() => import('/@/components/table/index.vue'));
const TableSearch = defineAsyncComponent(() => import('/@/components/table/search.vue'));

const uploaddialog = defineAsyncComponent(() => import('/@/components/table/uploaddialog.vue'));

const uploaddialogRef = ref();
const loading = ref();

// 页面加载时
onMounted(async () => {
});
const tableRef = ref<RefType>();
const editDialogRef = ref();
const DetailRef = ref();
const state = reactive({
  editprojectResultTitle: '',
  selectlist: [] as EmptyObjectType[],
  groupList: [] as Array<String>
  ,
});

const tb = reactive<TableDemoState>
  ({
    tableData: {

      // 表头内容（必传，注意格式）
      columns: [
        { prop: 'projectNo', width: 130, label: t('message.projectResult.projectNo'), headerAlign: 'center', toolTip: true, sortable: 'custom', isCheck: true },
        { prop: 'gCode', width: 130, label: t('message.projectResult.gCode'), headerAlign: 'center', toolTip: true, sortable: 'custom', isCheck: true },
        //{ prop: 'deviceList', width: 130, label: t('message.projectResult.deviceList'), headerAlign: 'center', toolTip: true, sortable: 'custom', isCheck: true },
        { prop: 'vmPathNew', width: 130, label: t('message.projectResult.vMPathNew'), headerAlign: 'center', toolTip: true, sortable: 'custom', isCheck: true },
        { prop: 'cadPathNew', width: 130, label: t('message.projectResult.cADPathNew'), headerAlign: 'center', toolTip: true, sortable: 'custom', isCheck: true },
        { prop: 'efficiency', width: 130, label: t('message.projectResult.efficiency'), headerAlign: 'center', toolTip: true, sortable: 'custom', isCheck: true },
        { prop: 'robotModel', width: 130, label: t('message.projectResult.robotModel'), headerAlign: 'center', toolTip: true, sortable: 'custom', isCheck: true },
        { prop: 'robotQty', width: 130, label: t('message.projectResult.robotQty'), headerAlign: 'center', toolTip: true, sortable: 'custom', isCheck: true },
        { prop: 'blanketMode', width: 130, label: t('message.projectResult.blanketMode'), headerAlign: 'center', toolTip: true, sortable: 'custom', isCheck: true },
        { prop: 'binType', width: 130, label: t('message.projectResult.binType'), headerAlign: 'center', toolTip: true, sortable: 'custom', isCheck: true },
        { prop: 'binSize', width: 130, label: t('message.projectResult.binSize'), headerAlign: 'center', toolTip: true, sortable: 'custom', isCheck: true },
        { prop: 'chargeQty', width: 130, label: t('message.projectResult.chargeQty'), headerAlign: 'center', toolTip: true, sortable: 'custom', isCheck: true },
        { prop: 'binQty', width: 130, label: t('message.projectResult.binQty'), headerAlign: 'center', toolTip: true, sortable: 'custom', isCheck: true },
        { prop: 'packQty', width: 130, label: t('message.projectResult.packQty'), headerAlign: 'center', toolTip: true, sortable: 'custom', isCheck: true },
        { prop: 'length', width: 130, label: t('message.projectResult.length'), headerAlign: 'center', toolTip: true, sortable: 'custom', isCheck: true },
        { prop: 'width', width: 130, label: t('message.projectResult.width'), headerAlign: 'center', toolTip: true, sortable: 'custom', isCheck: true },
        { prop: 'robotCode', width: 130, label: t('message.projectResult.robotCode'), headerAlign: 'center', toolTip: true, sortable: 'custom', isCheck: true },
        { prop: 'blanketCode', width: 130, label: t('message.projectResult.blanketCode'), headerAlign: 'center', toolTip: true, sortable: 'custom', isCheck: true },
        { prop: 'binTypeCode', width: 130, label: t('message.projectResult.binTypeCode'), headerAlign: 'center', toolTip: true, sortable: 'custom', isCheck: true },
        { prop: 'chargeCode', width: 130, label: t('message.projectResult.chargeCode'), headerAlign: 'center', toolTip: true, sortable: 'custom', isCheck: true },
        { prop: 'totalPrice', width: 130, label: t('message.projectResult.totalPrice'), headerAlign: 'center', toolTip: true, sortable: 'custom', isCheck: true },
        { prop: 'createUserName', width: 130, label: t('message.projectResult.createUserName'), headerAlign: 'center', toolTip: true, sortable: 'custom', isCheck: true },
        { prop: 'updateUserName', width: 130, label: t('message.projectResult.updateUserName'), headerAlign: 'center', toolTip: true, sortable: 'custom', isCheck: true },
        { prop: 'action', width: 140, label: t('message.pageConfiguration.operate'), type: 'action', align: 'center', isCheck: true, fixed: 'right', hideCheck: true },
      ],
      // 配置项（必传）
      config: {
        isStripe: true, // 是否显示表格斑马纹
        isBorder: false, // 是否显示表格边框
        isSerialNo: false, // 是否显示表格序号
        isSelection: true, // 是否勾选表格多选
        showSelection: auth('projectResult:batchDelete'), //是否显示表格多选
        pageSize: 20, // 每页条数
        hideExport: false, //是否隐藏导出按钮
        exportFileName: 'projectResult', //导出报表的文件名，不填写取应用名称
      },
      // 搜索表单，动态生成（传空数组时，将不显示搜索，type有3种类型：input,date,select）
      search: [
        { label: t('message.projectResult.projectNo'), prop: 'projectNo', placeholder: t('message.projectResult.projectNo'), required: false, type: 'input' },
        { label: t('message.projectResult.gCode'), prop: 'gCode', placeholder: t('message.projectResult.gCode'), required: false, type: 'input' },
        { label: t('message.projectResult.deviceList'), prop: 'deviceList', placeholder: t('message.projectResult.deviceList'), required: false, type: 'input' },
        { label: t('message.projectResult.vMPathNew'), prop: 'vMPathNew', placeholder: t('message.projectResult.vMPathNew'), required: false, type: 'input' },
        { label: t('message.projectResult.cADPathNew'), prop: 'cADPathNew', placeholder: t('message.projectResult.cADPathNew'), required: false, type: 'input' },
        { label: t('message.projectResult.efficiency'), prop: 'efficiency', placeholder: t('message.projectResult.efficiency'), required: false, type: 'input' },
        { label: t('message.projectResult.robotModel'), prop: 'robotModel', placeholder: t('message.projectResult.robotModel'), required: false, type: 'input' },
        { label: t('message.projectResult.robotQty'), prop: 'robotQty', placeholder: t('message.projectResult.robotQty'), required: false, type: 'input' },
        { label: t('message.projectResult.blanketMode'), prop: 'blanketMode', placeholder: t('message.projectResult.blanketMode'), required: false, type: 'input' },
        { label: t('message.projectResult.binType'), prop: 'binType', placeholder: t('message.projectResult.binType'), required: false, type: 'input' },
        { label: t('message.projectResult.binSize'), prop: 'binSize', placeholder: t('message.projectResult.binSize'), required: false, type: 'input' },
        { label: t('message.projectResult.length'), prop: 'length', placeholder: t('message.projectResult.length'), required: false, type: 'input' },
        { label: t('message.projectResult.width'), prop: 'width', placeholder: t('message.projectResult.width'), required: false, type: 'input' },
        { label: t('message.projectResult.robotCode'), prop: 'robotCode', placeholder: t('message.projectResult.robotCode'), required: false, type: 'input' },
        { label: t('message.projectResult.blanketCode'), prop: 'blanketCode', placeholder: t('message.projectResult.blanketCode'), required: false, type: 'input' },
        { label: t('message.projectResult.binTypeCode'), prop: 'binTypeCode', placeholder: t('message.projectResult.binTypeCode'), required: false, type: 'input' },
        { label: t('message.projectResult.chargeCode'), prop: 'chargeCode', placeholder: t('message.projectResult.chargeCode'), required: false, type: 'input' },
        { label: t('message.projectResult.totalPrice'), prop: 'totalPrice', placeholder: t('message.projectResult.totalPrice'), required: false, type: 'input' },
        { label: t('message.projectResult.createUserName'), prop: 'createUserName', placeholder: t('message.projectResult.createUserName'), required: false, type: 'input' },
        { label: t('message.projectResult.updateUserName'), prop: 'updateUserName', placeholder: t('message.projectResult.updateUserName'), required: false, type: 'input' },
      ],
      param: {},
      defaultSort: {
        prop: 'createTime',
        order: 'ascending',
      },
    },
  });
// 监听语言切换
watch(locale, async () => {
  // 更新表头和搜索表单的国际化文本
  tb.tableData.columns.forEach(column => {
    switch (column.prop) {
      case 'id':
        column.label = t('message.projectResult.id');
        break;
      case 'projectNo':
        column.label = t('message.projectResult.projectNo');
        break;
      case 'gCode':
        column.label = t('message.projectResult.gCode');
        break;
      case 'deviceList':
        column.label = t('message.projectResult.deviceList');
        break;
      case 'vMPathNew':
        column.label = t('message.projectResult.vMPathNew');
        break;
      case 'cADPathNew':
        column.label = t('message.projectResult.cADPathNew');
        break;
      case 'efficiency':
        column.label = t('message.projectResult.efficiency');
        break;
      case 'robotModel':
        column.label = t('message.projectResult.robotModel');
        break;
      case 'robotQty':
        column.label = t('message.projectResult.robotQty');
        break;
      case 'blanketMode':
        column.label = t('message.projectResult.blanketMode');
        break;
      case 'binType':
        column.label = t('message.projectResult.binType');
        break;
      case 'binSize':
        column.label = t('message.projectResult.binSize');
        break;
      case 'chargeQty':
        column.label = t('message.projectResult.chargeQty');
        break;
      case 'binQty':
        column.label = t('message.projectResult.binQty');
        break;
      case 'packQty':
        column.label = t('message.projectResult.packQty');
        break;
      case 'length':
        column.label = t('message.projectResult.length');
        break;
      case 'width':
        column.label = t('message.projectResult.width');
        break;
      case 'robotCode':
        column.label = t('message.projectResult.robotCode');
        break;
      case 'blanketCode':
        column.label = t('message.projectResult.blanketCode');
        break;
      case 'binTypeCode':
        column.label = t('message.projectResult.binTypeCode');
        break;
      case 'chargeCode':
        column.label = t('message.projectResult.chargeCode');
        break;
      case 'totalPrice':
        column.label = t('message.projectResult.totalPrice');
        break;
      case 'createUserName':
        column.label = t('message.projectResult.createUserName');
        break;
      case 'updateUserName':
        column.label = t('message.projectResult.updateUserName');
        break;
      case 'action':
        column.label = t('message.pageConfiguration.operate');
        break;
    }
  });

  // 更新搜索表单的国际化文本
  tb.tableData.search.forEach(searchItem => {
    switch (searchItem.prop) {
      case 'id':
        searchItem.label = t('message.projectResult.id');
        searchItem.placeholder = t('message.projectResult.id');
        break;
      case 'projectNo':
        searchItem.label = t('message.projectResult.projectNo');
        searchItem.placeholder = t('message.projectResult.projectNo');
        break;
      case 'gCode':
        searchItem.label = t('message.projectResult.gCode');
        searchItem.placeholder = t('message.projectResult.gCode');
        break;
      case 'deviceList':
        searchItem.label = t('message.projectResult.deviceList');
        searchItem.placeholder = t('message.projectResult.deviceList');
        break;
      case 'vMPathNew':
        searchItem.label = t('message.projectResult.vMPathNew');
        searchItem.placeholder = t('message.projectResult.vMPathNew');
        break;
      case 'cADPathNew':
        searchItem.label = t('message.projectResult.cADPathNew');
        searchItem.placeholder = t('message.projectResult.cADPathNew');
        break;
      case 'efficiency':
        searchItem.label = t('message.projectResult.efficiency');
        searchItem.placeholder = t('message.projectResult.efficiency');
        break;
      case 'robotModel':
        searchItem.label = t('message.projectResult.robotModel');
        searchItem.placeholder = t('message.projectResult.robotModel');
        break;
      case 'robotQty':
        searchItem.label = t('message.projectResult.robotQty');
        searchItem.placeholder = t('message.projectResult.robotQty');
        break;
      case 'blanketMode':
        searchItem.label = t('message.projectResult.blanketMode');
        searchItem.placeholder = t('message.projectResult.blanketMode');
        break;
      case 'binType':
        searchItem.label = t('message.projectResult.binType');
        searchItem.placeholder = t('message.projectResult.binType');
        break;
      case 'binSize':
        searchItem.label = t('message.projectResult.binSize');
        searchItem.placeholder = t('message.projectResult.binSize');
        break;
      case 'chargeQty':
        searchItem.label = t('message.projectResult.chargeQty');
        searchItem.placeholder = t('message.projectResult.chargeQty');
        break;
      case 'binQty':
        searchItem.label = t('message.projectResult.binQty');
        searchItem.placeholder = t('message.projectResult.binQty');
        break;
      case 'packQty':
        searchItem.label = t('message.projectResult.packQty');
        searchItem.placeholder = t('message.projectResult.packQty');
        break;
      case 'length':
        searchItem.label = t('message.projectResult.length');
        searchItem.placeholder = t('message.projectResult.length');
        break;
      case 'width':
        searchItem.label = t('message.projectResult.width');
        searchItem.placeholder = t('message.projectResult.width');
        break;
      case 'robotCode':
        searchItem.label = t('message.projectResult.robotCode');
        searchItem.placeholder = t('message.projectResult.robotCode');
        break;
      case 'blanketCode':
        searchItem.label = t('message.projectResult.blanketCode');
        searchItem.placeholder = t('message.projectResult.blanketCode');
        break;
      case 'binTypeCode':
        searchItem.label = t('message.projectResult.binTypeCode');
        searchItem.placeholder = t('message.projectResult.binTypeCode');
        break;
      case 'chargeCode':
        searchItem.label = t('message.projectResult.chargeCode');
        searchItem.placeholder = t('message.projectResult.chargeCode');
        break;
      case 'totalPrice':
        searchItem.label = t('message.projectResult.totalPrice');
        searchItem.placeholder = t('message.projectResult.totalPrice');
        break;
      case 'createUserName':
        searchItem.label = t('message.projectResult.createUserName');
        searchItem.placeholder = t('message.projectResult.createUserName');
        break;
      case 'updateUserName':
        searchItem.label = t('message.projectResult.updateUserName');
        searchItem.placeholder = t('message.projectResult.updateUserName');
        break;
    }
  });

  // 等待下一次 DOM 更新循环完成后再进行表格刷新
  await nextTick();
  tableRef.value?.handleList(); // 刷新表格数据
});
const getData = async (param: any) => {

  loading.value = true;
  var res = await pageProjectResult(Object.assign(param));

  loading.value = false;
  return res.data;
};
const exportChangeData = (data: Array<EmptyObjectType>
) => {
  data.forEach((item) => {
    item.sysFlag = item.sysFlag == 1 ? t('message.projectResult.yes') : t('message.projectResult.no');
  });
  return data;
};
// 拖动显示列排序回调
const onSortHeader = (data: object[]) => {
  tb.tableData.columns = data;
};
// 搜索点击时表单回调
const onSearch = (data: EmptyObjectType) => {
  tb.tableData.param = Object.assign({}, tb.tableData.param, { ...data });
  nextTick(() => {
    tableRef.value.pageReset();
  });
};

const getGroupList = async () => {

};

//表格多选事件
const tableSelection = (data: EmptyObjectType[]) => {
  // console.log('表格多选事件', data)
  state.selectlist = data;
};

onMounted(async () => {
  getGroupList();
});

// 更新数据
const updateData = () => {
  tableRef.value.handleList();
  getGroupList();
};
const opendetail = (row:any) => {
  console.log('detail',row)
  DetailRef.value?.openDialog(row);
};

// 打开新增页面
const openaddprojectResult = () => {
  state.editprojectResultTitle = t('message.projectResult.addprojectResult');
  editDialogRef.value?.openDialog({});
};

// 打开编辑页面
const openedit = (row: any) => {
  state.editprojectResultTitle = t('message.projectResult.editprojectResult');
  editDialogRef.value?.openDialog(row);
};

// 删除
const delConfig = (row: any) => {
  ElMessageBox.confirm(t('message.messages.confirmDel') + `：【${row[tb.tableData.columns[1].prop.toString()]}】?`, t('message.messages.hint'), {
    confirmButtonText: t('message.messages.confirm'),
    cancelButtonText: t('message.messages.cancel'),
    type: 'warning',
  })
    .then(async () => {
      await deleteProjectResult({ id: row.id });
      tableRef.value.handleList();
      ElMessage.success(t('message.messages.deleteSuccess'));
    })
    .catch(() => { });
};

//批量删除
const bacthDelete = () => {
  if (state.selectlist.length == 0) return false;
  ElMessageBox.confirm(t('message.messages.confirmBatchDel') + `【${state.selectlist[0].name}】${state.selectlist.length}` + t('message.messages.con'), t('message.messages.hint'), {
    confirmButtonText: t('message.messages.confirm'),
    cancelButtonText: t('message.messages.cancel'),
    type: 'warning',
  })
    .then(async () => {
      const ids = state.selectlist.map((item) => {
        return item.id;
      });
      var res = await batchDeleteProjectResult(ids);
      tableRef.value.pageReset();
      ElMessage.success(t('message.messages.deleteSuccess'));
    })
    .catch(() => { });



};
// 打开上传页面
const openUploadDialog = () => {
  uploaddialogRef.value.openUploadDialog();
};
const importData = async (param: any) => {
  var res = await importProjectResult(param);
  return res;
};

const downTemplete = async () => {
  var res = await downLoadProjectResult();
  downFile(res);
}     
</script>
